/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DaveSexton.DocProject;
using VSLangProj80;
using System.ComponentModel;
using DaveSexton.DocProject.Sandcastle;
using System.Drawing.Design;
using System.Web.UI.Design;

namespace DaveSexton.DocProject.DeploymentSandcastle
{
	public class DeploymentSandcastleProjectOptions : SandcastleProjectOptions
	{
		#region Public Properties
		[Category("Deployment"), DefaultValue(false), DisplayName("Enabled"), SortedProperty(20), 
		Description("Indicates whether the build output should be deployed to the deployment location.")]
		public bool DeploymentEnabled
		{
			get
			{
				return Project.Settings.Read<bool>("DeploymentSandcastle_DeploymentEnabled");
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentEnabled", value);
			}
		}

		[Category("Deployment"), DisplayName("Content"), SortedProperty(40),
		Description("Indicates the source content that will be deployed to the Target Location.")]
		public DeploymentContentTypes DeploymentContent
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentContent", DeploymentContentTypes.Chm | DeploymentContentTypes.Hxs);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentContent", value);
			}
		}

		[Category("Deployment"), DefaultValue(null), DisplayName("Target Location"), SortedProperty(60), 
		Description("Specifies the path to where the build output will be deployed, excluding the file name.  Local file and UNC paths, HTTP, HTTPS and FTP urls are supported, as well as any other Uri schemes that are registered at runtime with the WebRequest.RegisterPrefix method.")]
		public Uri DeploymentLocation
		{
			get
			{
				return Project.Settings.Read<Uri>("DeploymentSandcastle_DeploymentLocation");
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentLocation", value);
			}
		}

		[Category("Deployment Authentication"), DefaultValue(true), DisplayName("Use default credentials"), SortedProperty(20), 
		Description("Indicates whether the default credentials should be used for deployment.  Set this property to false to specify credentials using the deployment user name and password properties.  Note: explicit credentials are ignored if the target location is a local file or UNC path.")]
		public bool DeploymentUseDefaultCredentials
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentUseDefaultCredentials", true);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentUseDefaultCredentials", value);
			}
		}

		[Category("Deployment Authentication"), DefaultValue(null), DisplayName("User name"), SortedProperty(40), 
		Description("Specifies the user name that should be used for deployment.  This property is only used when the \"Use default credentials\" property is false.  Note: credentials are ignored if the target location is a local file or UNC path.")]
		public string DeploymentUserName
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentUserName");
			}
			set
			{
				Project.Settings["DeploymentSandcastle_DeploymentUserName"] = value;
			}
		}

		[Category("Deployment Authentication"), DefaultValue(null), DisplayName("Password"), SortedProperty(60), 
		Description("Specifies the password that should be used for deployment.  This property is only used when the \"Use default credentials\" property is false.  Note: credentials are ignored if the target location is a local file or UNC path..")]
		public string DeploymentPassword
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentPassword");
			}
			set
			{
				Project.Settings["DeploymentSandcastle_DeploymentPassword"] = value;
			}
		}

		[Category("Deployment Proxy"), DisplayName("Use Proxy"), SortedProperty(20), 
		Description("Indicates whether the proxy information will be used for deployment.")]
		public bool DeploymentUseProxy
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentUseProxy",
					DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseProxy);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentUseProxy", value);
			}
		}

		[Category("Deployment Proxy"), DisplayName("Proxy Server"), SortedProperty(40), 
		Description("Specifies the location of the proxy that will be used for deployment.")]
		public Uri DeploymentProxy
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentProxy",
					DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxy);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentProxy", value);
			}
		}

		[Category("Deployment Proxy Authentication"), DisplayName("Use default credentials"), SortedProperty(20), 
		Description("Indicates whether the default credentials should be used for the deployment proxy.  Set this property to false to specify credentials using the proxy user name and password properties.")]
		public bool DeploymentUseDefaultProxyCredentials
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentUseDefaultProxyCredentials",
					DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseDefaultProxyCredentials);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentUseDefaultProxyCredentials", value);
			}
		}

		[Category("Deployment Proxy Authentication"), DisplayName("User name"), SortedProperty(40), 
		Description("Specifies the proxy user name that should be used for deployment.  This property is only used when the proxy \"Use default credentials\" property is false.")]
		public string DeploymentProxyUserName
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentProxyUserName",
					DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyUserName);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentProxyUserName", value);
			}
		}

		[Category("Deployment Proxy Authentication"), DisplayName("Password"), SortedProperty(60), 
		Description("Specifies the proxy password that should be used for deployment.  This property is only used when the proxy \"Use default credentials\" property is false.")]
		public string DeploymentProxyPassword
		{
			get
			{
				return Project.Settings.Read("DeploymentSandcastle_DeploymentProxyPassword",
					DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyPassword);
			}
			set
			{
				Project.Settings.Write("DeploymentSandcastle_DeploymentProxyPassword", value);
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DeploymentSandcastleProjectOptions" /> class.
		/// </summary>
		public DeploymentSandcastleProjectOptions(IDocProject project)
			: base(project)
		{
		}
		#endregion

		#region Methods
		// the following methods are for property grid reflection

		private bool ShouldSerializeDeploymentContent()
		{
      string content = Project.Settings.GetCommittedValue("DeploymentSandcastle_DeploymentContent");

      if (string.IsNullOrEmpty(content))
        return Project.Settings.ContainsKey("DeploymentSandcastle_DeploymentContent");
      else
        return DeploymentContent != (DeploymentContentTypes) Enum.Parse(typeof(DeploymentContentTypes), content);
		}

		private void ResetDeploymentContent()
		{
      string content = Project.Settings.GetCommittedValue("DeploymentSandcastle_DeploymentContent");

      if (!string.IsNullOrEmpty(content))
        DeploymentContent = (DeploymentContentTypes) Enum.Parse(typeof(DeploymentContentTypes), content);
      else
        Project.Settings.Remove("DeploymentSandcastle_DeploymentContent");
		}

		private bool ShouldSerializeDeploymentUseProxy()
		{
			return DeploymentUseProxy != DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseProxy;
		}

		private void ResetDeploymentUseProxy()
		{
			DeploymentUseProxy = DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseProxy;
		}

		private bool ShouldSerializeDeploymentProxy()
		{
			return DeploymentProxy != DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxy;
		}

		private void ResetDeploymentProxy()
		{
			DeploymentProxy = DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxy;
		}

		private bool ShouldSerializeDeploymentUseDefaultProxyCredentials()
		{
			return DeploymentUseDefaultProxyCredentials != DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseDefaultProxyCredentials;
		}

		private void ResetDeploymentUseDefaultProxyCredentials()
		{
			DeploymentUseDefaultProxyCredentials = DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentUseDefaultProxyCredentials;
		}

		private bool ShouldSerializeDeploymentProxyUserName()
		{
			return DeploymentProxyUserName != DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyUserName;
		}

		private void ResetDeploymentProxyUserName()
		{
			DeploymentProxyUserName = DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyUserName;
		}

		private bool ShouldSerializeDeploymentProxyPassword()
		{
			return DeploymentProxyPassword != DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyPassword;
		}

		private void ResetDeploymentProxyPassword()
		{
			DeploymentProxyPassword = DocProjectEnvironment.GetProvider<DeploymentSandcastleBuildEngineProvider>(false).DeploymentProxyPassword;
		}
		#endregion
	}
}
